package com.quan.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quan.admin.entity.SysMenu;
import com.quan.admin.mapper.SysMenuMapper;
import com.quan.admin.service.SysMenuService;
import com.quan.admin.service.SysPermissionService;
import com.quan.admin.service.SysRolePermissionService;
import com.quan.commons.core.biz.support.MyBaseServiceImpl;
import com.quan.commons.core.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class SysMenuServiceImpl extends MyBaseServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
	private SysMenuMapper mapper;


    @Autowired
    private SysRolePermissionService sysRolePermissionService;

    @Autowired
    private SysPermissionService systemPermissionService;

    @Override
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            this.recursiveDelete(id);
        }
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {
        return this.recursiveDelete(id);
    }

    private boolean recursiveDelete(Serializable id) {
        System.out.println("===root==>" + id);
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<SysMenu>();
        queryWrapper.eq("pid", id);

        List<SysMenu> child = super.list(queryWrapper) ;
        if(null != child) {
            for (SysMenu entity : child) {
                System.out.println("===child==>" + entity.getId());
                this.systemPermissionService.deleteByMenuId(entity.getId());
                recursiveDelete(entity.getId());
            }
        }

        this.systemPermissionService.deleteByMenuId(id);

        //删除与角色关联的数据
        this.sysRolePermissionService.deleteByMenuId(id);

        return super.removeById(id);
    }

	@Override
    public IPage<SysMenu> selectMyPage(PageUtils pageUtils, SysMenu entity) {
        Page<SysMenu> page = new Page<SysMenu>(pageUtils.getPage(), pageUtils.getLimit());

        // 排序
        super.handleSort(page, pageUtils);

        return this.mapper.selectMyPage(page, entity);
    }

    @Override
    public List<SysMenu> listWithTree() {
        // 1、查出所有数据
        List<SysMenu> entityes = super.list();

        // 2、组装成父子的树形结构
        List<SysMenu> list = entityes.stream().filter((entity) -> {
            // 1、找到所有一级分类
            return entity.getPid() == 0;
        }).map((child) -> {
            // 2、找到所有子节点
            return child.setChildren(getChildren(child, entityes));
        }).sorted((m1, m2) -> {
            // 3、排序
            return (m1.getSort() == null ? 0 : m1.getSort()) - (m2.getSort() == null ? 0 : m2.getSort());
        }).collect(Collectors.toList());

        return list;
    }

    /**
     * 递归查找所有子节点
     * @param root
     * @param all
     * @return
     */
    public List<SysMenu> getChildren(SysMenu root, List<SysMenu> all) {
        List<SysMenu> children = all.stream().filter((entity) -> {
            return entity.getPid() == root.getId();
        }).map((entity) -> {
            // 1、递归找到子节点
            return entity.setChildren(getChildren(entity, all));
        }).sorted((m1, m2) -> {
            // 2、排序
            return (m1.getSort() == null ? 0 : m1.getSort()) - (m2.getSort() == null ? 0 : m2.getSort());
        }).collect(Collectors.toList());

        return children;
    }

    @Override
    public List<SysMenu> listWithTreeOnPermission() {
        // 1、查出所有数据
        List<SysMenu> entityes = super.list();

        // 2、组装成父子的树形结构
        List<SysMenu> list = entityes.stream().filter((entity) -> {
            entity.setPermission(this.systemPermissionService.findPermissionByMenuId(entity.getId()));
            // 1、找到所有一级分类
            return entity.getPid() == 0;
        }).map((child) -> {
            // 2、找到所有子节点
            return child.setChildren(getChildrenOnPermission(child, entityes));
        }).sorted((m1, m2) -> {
            // 3、排序
            return (m1.getSort() == null ? 0 : m1.getSort()) - (m2.getSort() == null ? 0 : m2.getSort());
        }).collect(Collectors.toList());

        return list;
    }

    /**
     * 递归查找所有子节点
     * @param root
     * @param all
     * @return
     */
    public List<SysMenu> getChildrenOnPermission(SysMenu root, List<SysMenu> all) {
        List<SysMenu> children = all.stream().filter((entity) -> {
            return entity.getPid() == root.getId();
        }).map((entity) -> {
            entity.setPermission(this.systemPermissionService.findPermissionByMenuId(entity.getId()));
            // 1、递归找到子节点
            return entity.setChildren(getChildrenOnPermission(entity, all));
        }).sorted((m1, m2) -> {
            // 2、排序
            return (m1.getSort() == null ? 0 : m1.getSort()) - (m2.getSort() == null ? 0 : m2.getSort());
        }).collect(Collectors.toList());

        return children;
    }

}